﻿<?php
class jobs_mod extends CI_Model 
{
	function create_job($data)
	{
		$insert = $this->db->insert('jobs', $data);
		$data['lastid'] = $this->db->insert_id();
		return $data;
	}
	function create_job_tag($data,$j_id)
	{
		$insert = $this->db->insert('job_tags', $data);
		$data=$this->db->insert_id();
		
		//notification if found get the u_id of the tag and email him!
		$array_of_match=array();
		$keyword=$data['j_tag'];//new job tags
		$k=explode(',',$keyword);
		$this->db->like('t_name',$keyword);
		foreach($k as $word)
		{
			$this->db->like('t_name',$word);
		}
		$this->db->select('*');
		$this->db->from('tags');
		$this->db->join('users', 'users.u_id = tags.u_id');
		$query = $this->db->get();
		
		foreach ($query->result_array() as $row)
		{
			//send email
			
			$to = $row['u_email'];
			$subject = "فرصة جديدة تناسب تاقاتك - باب رزق";
			$body = "هذا الرابط للفرصة \n\r ".base_url().'index.php/index.php/jobs_cont/view_job/'.$j_id;
			if (mail($to, $subject, $body)) 
			{
				echo("<p>Message successfully sent!</p>");
			} 
			else 
			{
				echo("<p>Message delivery failed...</p>");
			}
			
			//end send email
		}
		
		


		//end of notification
		
		return $data;
	}
	function get_job($id)
	{
		$this->db->where("j_id", $id); 
		return $this->db->get('jobs');
	}
	function add_abuse($data)
	{
		$insert = $this->db->insert('j_abuse', $data);
		return $this->db->insert_id();
	}
	function same_user_abused_same_job($id,$ip)
	{
		$this->db->where('j_id', $id);
		$this->db->where('ab_owner_ip', $ip);
		$query = $this->db->get('j_abuse');
		
		if($query->num_rows() == 1)
		{
			return true;
		}
		else
			return false;
	}
	function apply($id)
	{
		
		$this->db->where('j_id', $id);
		$curr_applied=$this->db->get('jobs');
		foreach($curr_applied->result_array()as $row)
		{
			$current_applied_count=$row['j_applied'];
		}
		$data=array('j_applied'=>$current_applied_count+1);
		$this->db->where('j_id', $id);
		$query = $this->db->update('jobs',$data);
		
	}
	function latest_jobs()
	{
		$this->db->order_by("j_modification_date", "desc"); 
		return $this->db->get('jobs',100);
		return $q;
	}
}